package com.rh.service.impl;

import com.rh.entity.CourseOrderVo;
import com.rh.entity.TOrder;
import com.rh.entity.UcenterMemberOrderVo;
import com.rh.feign.CourseFeignService;
import com.rh.feign.UcenterFeignService;
import com.rh.mapper.TOrderMapper;
import com.rh.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rh.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 订单 服务实现类
 * @author rh
 * @since 2021-11-15
 */
@Service
@Transactional
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {
    //注入远程调用服务
    @Resource
    private CourseFeignService courseFeignService;
    @Resource
    private UcenterFeignService ucenterFeignService;
    /**
     * 生成订单的方法
     * @param courseId
     * @param memberId
     * @return
     */
    @Override
    public String createOrders(String courseId, String memberId) {
        //远程调用根据用户id获取用户信息
        UcenterMemberOrderVo memberInfoById = ucenterFeignService.getMemberInfoById(memberId);
        //远程调用根据课程id获取课程信息
        CourseOrderVo courseInfo = courseFeignService.getCourseInfo(courseId);
        //创建订单对象并设置对应数据的值
        TOrder tOrder = new TOrder();

        //订单号,工具类生成唯一的订单号
        tOrder.setOrderNo(OrderNoUtil.getOrderNo());
        //课程Id
        tOrder.setCourseId(courseId);
        //课程标题
        tOrder.setCourseTitle(courseInfo.getTitle());
        //课程图片
        tOrder.setCourseCover(courseInfo.getCover());
        //讲师名称
        tOrder.setTeacherName(courseInfo.getTeacherName());
        //价格
        tOrder.setTotalFee(courseInfo.getPrice());
        //用户id
        tOrder.setMemberId(memberId);
        //会员账号
        tOrder.setMobile(memberInfoById.getMobile());
        //昵称
        tOrder.setNickname(memberInfoById.getNickname());

        //是否支付 1已支付 0未支付
        tOrder.setStatus(0);
        //支付类型,1微信 2支付宝
        tOrder.setPayType(1);

        baseMapper.insert(tOrder);
        //生成订单后返回一个订单号,直接get
        return tOrder.getOrderNo();
    }
}
